package com.example.park2212.park

import android.util.Log
import androidx.appcompat.widget.SearchView.OnQueryTextListener
import androidx.core.view.GravityCompat
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter

import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.ParkBeanItem
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect

import kotlinx.coroutines.launch

@Route(path = "/app/park/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding, ParkViewModel>() {

    val parkPageAdapter by lazy { ParkPageAdapter() }
    var areaId = "-1"
    var type = ""

    override fun initView() {
        binding.headParkAdd.headerTitle.setText("车位管理")
        binding.headParkAdd.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/app/ParkAddActivity").navigation()
        }
        binding.rvPark.apply {
            adapter = parkPageAdapter
            layoutManager = LinearLayoutManager(this@ParkActivity)
        }
        parkPageAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(position: Int, item: ParkBeanItem) {
                ARouter.getInstance().build("/app/park/ParkEditActivity")
                    .withSerializable("bean",item).navigation()
            }
        })

        parkPageAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(item: ParkBeanItem) {
                ToastUtils.showLong(item.toString())
                ARouter.getInstance().build("/app/park/ParkVRActivity").navigation()
            }
        })
        lifecycleScope.launch {
            viewModel.getParksByType("标准车位","类型").collect{
                parkPageAdapter.submitData(it)
            }
        }

        binding.parkFilter.setOnClickListener {
            binding.drawer.openDrawer(GravityCompat.END)
        }

        binding.drawRg.setOnCheckedChangeListener { group, checkId ->
            binding.drawer.closeDrawer(GravityCompat.END)
            var status = ""
            if (checkId== R.id.rb_kx){
                status="空闲"
            }else if (checkId==R.id.rb_zy){
                status="已占用"
            } else {
                status="维护中"
            }
            lifecycleScope.launch {
                viewModel.getParksByType(status,"状态").collect{
                    parkPageAdapter.submitData(it)
                }
            }
        }

        binding.parkSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                areaId = query!!
                if (type.isNotEmpty()){
                    lifecycleScope.launch {
                        viewModel.getParksByType(areaId,type).collect{
                            parkPageAdapter.submitData(it)
                        }
                    }
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                return true
            }
        })

        binding.drawArea.setOnClickListener {
            type="区域"
            binding.drawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                if (areaId=="-1"){
                    ToastUtils.showLong("请输入区域ID")
                    return@launch
                }
                viewModel.getParksByType(areaId,type).collect{
                    parkPageAdapter.submitData(it)
                }
            }
        }
        binding.drawType.setOnClickListener {
            type="类型"
            binding.drawer.closeDrawer(GravityCompat.END)
            lifecycleScope.launch {
                if (areaId=="-1"){
                    ToastUtils.showLong("请输入类型")
                    return@launch
                }
                viewModel.getParksByType(areaId,type).collect{
                    parkPageAdapter.submitData(it)
                }
            }
        }





    }

    override fun initData() {

    }


}